package com.zbkj.service.helper.region;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.zbkj.common.model.admin.SystemAdmin;
import com.zbkj.common.model.merchant.Merchant;
import com.zbkj.common.model.zfbadminregion.ZfbAdminRegion;
import com.zbkj.common.region.SearchRegionVo;
import com.zbkj.common.utils.SecurityUtil;
import com.zbkj.service.dao.MerchantDao;
import com.zbkj.service.service.adminregion.IZfbAdminRegionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: RegionHelper
 * @Description: TODO
 * @Author: fanbing
 * @Date: 2024/8/4 14:44
 * @Version: V1.0
 **/
@Component
@Slf4j
public class RegionHelper {
    @Autowired
    private IZfbAdminRegionService zfbAdminRegionService;

    @Autowired
    private MerchantDao merchantDao;


    public SearchRegionVo getCurrentUserRegion() {
        SystemAdmin user = SecurityUtil.getLoginUserVo().getUser();
        log.info("#getCurrentUserRegion#查询当前用户信息user={}", JSON.toJSONString(user));

        List<ZfbAdminRegion> list = zfbAdminRegionService.list(new LambdaQueryWrapper<ZfbAdminRegion>().eq(ZfbAdminRegion::getAdminUserId, user.getId()));
        if (CollectionUtils.isEmpty(list)) {
            return new SearchRegionVo();
        }

        List<String> provinceIdList = Lists.newArrayList();
        List<String> cityIdList = Lists.newArrayList();
        List<String> districtIdList = Lists.newArrayList();
        List<String> townIdList = Lists.newArrayList();
        for (ZfbAdminRegion zfbAdminRegion : list) {

            if (StrUtil.isNotBlank(zfbAdminRegion.getTownId())) {
                townIdList.add(zfbAdminRegion.getTownId());
                continue;
            }
            if (StrUtil.isNotBlank(zfbAdminRegion.getDistrictId())) {
                districtIdList.add(zfbAdminRegion.getDistrictId());
                continue;
            }

            if (StrUtil.isNotBlank(zfbAdminRegion.getCityId())) {
                cityIdList.add(zfbAdminRegion.getCityId());
                continue;
            }

            if (StrUtil.isNotBlank(zfbAdminRegion.getProvinceId())) {
                provinceIdList.add(zfbAdminRegion.getProvinceId());
            }

        }

        SearchRegionVo searchRegionVo = new SearchRegionVo();
        searchRegionVo.setProvinceIdList(provinceIdList);
        searchRegionVo.setCityIdList(cityIdList);
        searchRegionVo.setDistrictIdList(districtIdList);
        searchRegionVo.setTownIdList(townIdList);

        //只要存在一个
        if (!CollectionUtils.isEmpty(provinceIdList)
                || !CollectionUtils.isEmpty(cityIdList)
                || !CollectionUtils.isEmpty(districtIdList)
                || !CollectionUtils.isEmpty(townIdList)) {
            searchRegionVo.setAreaNullFlag(Boolean.FALSE);
        }


        //查询负责的所有商户
        LambdaQueryWrapper<Merchant> qw = new LambdaQueryWrapper<>();
        qw.select(Merchant::getId);
        qw.in(!CollectionUtils.isEmpty(provinceIdList), Merchant::getProvinceId, provinceIdList);
        qw.or().in(!CollectionUtils.isEmpty(cityIdList), Merchant::getCityId, cityIdList);
        qw.or().in(!CollectionUtils.isEmpty(districtIdList), Merchant::getDistrictId, districtIdList);
        qw.or().in(!CollectionUtils.isEmpty(townIdList), Merchant::getTownId, townIdList);
        List<Merchant> merchants = merchantDao.selectList(qw);
        List<Integer> merchantIds = merchants.stream().map(Merchant::getId).collect(Collectors.toList());
        log.info("区域管理用户={}", merchantIds);
        searchRegionVo.setMerchantIds(merchantIds);

        if (!CollectionUtils.isEmpty(merchantIds)) {
            searchRegionVo.setMerchantIdNullFlag(Boolean.FALSE);
        }

        return searchRegionVo;
    }
}
